﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace MDT.LifeSewagePlatform.Conmmon
{
    public class SendEmail: ISendEmail
    {
        private ILogger<SendEmail> _logger;
        public SendEmail(ILogger<SendEmail> logger)
        {
            _logger = logger;
        }
        public async Task<bool> SendMessage(EmailModel email,string ToMial,string Content, string ReplyTo=null, string CCMial=null, string File_Path=null)
        {
           return await Task.Run(() =>
            {
                try
                {
                
                    MailMessage mailMessage = new MailMessage();
                    mailMessage.Priority = MailPriority.Normal;

                    mailMessage.From = new MailAddress(email.BindEmail);

                    List<string> ToMiallist = ToMial.Split(';').ToList();
                    for (int i = 0; i < ToMiallist.Count; i++)
                    {
                        mailMessage.To.Add(new MailAddress(ToMiallist[i]));  //收件人邮箱地址。
                    }

                    if (ReplyTo == "" || ReplyTo == null)
                    {
                        ReplyTo = email.BindEmail;
                    }

                    if (CCMial != "" && CCMial != null)
                    {
                        List<string> CCMiallist = ToMial.Split(';').ToList();
                        for (int i = 0; i < CCMiallist.Count; i++)
                        {
                            //邮件的抄送者，支持群发
                            mailMessage.CC.Add(new MailAddress(CCMial));
                        }
                    }
              
                    mailMessage.IsBodyHtml = false;

                    mailMessage.Subject = email.Subject;
                    mailMessage.Body = Content;
                    if (File_Path != "" && File_Path != null)
                    {
                        
                        mailMessage.Attachments.Add(new Attachment(File_Path));
                        mailMessage.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess;
                    }

                    SmtpClient client = new SmtpClient();

                    #region 设置邮件服务器地址

                    if (email.BindEmail.Length != 0)
                    {
                        string[] addressor = email.BindEmail.Trim().Split(new Char[] { '@', '.' });
                        switch (addressor[1])
                        {
                            case "163":
                                client.Host = "smtp.163.com";
                                break;
                            case "126":
                                client.Host = "smtp.126.com";
                                break;
                            case "qq":
                                client.Host = "smtp.qq.com";
                                break;
                            case "gmail":
                                client.Host = "smtp.gmail.com";
                                break;
                            case "hotmail":
                                client.Host = "smtp.live.com";//outlook邮箱
                                                           
                                break;
                            case "foxmail":
                                client.Host = "smtp.foxmail.com";
                                break;
                            case "sina":
                                client.Host = "smtp.sina.com.cn";
                                break;
                            default:
                                client.Host = "smtp.exmail.qq.com";//qq企业邮箱
                                break;
                        }
                    }
                    #endregion

                    //使用安全加密连接。
                    client.EnableSsl = true;
                    //不和请求一块发送。
                    client.UseDefaultCredentials = false;

                    //验证发件人身份(发件人的邮箱，邮箱里的生成授权码);
                    client.Credentials = new NetworkCredential(email.BindEmail, email.BindEmailCode);

                    //如果发送失败，SMTP 服务器将发送 失败邮件告诉我  
                    mailMessage.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
                    //发送
                    client.Send(mailMessage);
                    return true;
                }
                catch (Exception ex)
                {
                    _logger.LogInformation(ex.Message);
                    return false;
                }
            });

        }
    }
}
